package com.example.projecttest;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.tbruyelle.rxpermissions2.RxPermissions;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 工程测试用例
 */

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private String[] str={"动态权限","RxPermissions","封装权限申请","Fragment","OkHTTP","播放音乐","Retrofit"};
    private ArrayAdapter<String> mAdapter;
    private String[] permissionArray=new String[]{Manifest.permission.CALL_PHONE};

    /**
     * 1.动态权限
     *
     * 确定权限--> call phone
     * 判断权限
     * 申请权限
     * 处理权限
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mListView=$(R.id.mListView);
        mAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,str);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }

    public <T> T $(int viewID){
        return (T) findViewById(viewID);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 0:
                requstPermissions();
                break;

            case 1:
                RxPermissions();
                break;

            case 2:
                startActivity(new Intent(MainActivity.this,TestPermiss.class));
                break;
            case 3:
                startActivity(new Intent(MainActivity.this,FragmentTestActivity.class));
                break;
            case 4:
                startActivity(new Intent(MainActivity.this,OkHttpActivity.class));
                break;
            case 5:
                startActivity(new Intent(MainActivity.this,MusicActivity.class));
                break;
            case 6:
                startActivity(new Intent(MainActivity.this,RetrofitActivity.class));
                break;
        }
    }

    //打电话 Permission Denial
    private void callPhone(String phone){
        Intent intent=new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("tel:"+phone));
        startActivity(intent);
    }

    //普通的请求权限
    private void requstPermissions(){
        //判断权限 电话权限
        /**
         * permissionCheck
         * 返回值 -1 未授予权限
         * 返回值 0 已授予权限
         */
        int permissionCheck= ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
        if(permissionCheck!= PackageManager.PERMISSION_GRANTED){
            //判断用户是否拒绝过该权限
            /**
             * isPermiss
             * 返回值 true 拒绝过该权限
             * 返回值 false 未拒绝过该权限
             */
            boolean isPermiss= ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE);
            if(isPermiss){
                //说明拒绝过该权限|需要解释权限
                Toast.makeText(this, "不授予该权限无法拨打电话!", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this,permissionArray,100);
            }else {
                //申请权限
                ActivityCompat.requestPermissions(this,permissionArray,100);
            }
        }else{
            callPhone("18957806320");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        /**
         * grantResults[]授予结果
         * 返回 -1 拒绝
         * 返回 0 同意
         * permissions[] 权限数组
         * 返回要授予的权限
         */
        if(requestCode==100){
            if(grantResults.length>0){
                if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    callPhone("18957806320");
                }else {
                    Toast.makeText(this, "无法拨打电话!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    //RxPermissions
    private void RxPermissions(){
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.CALL_PHONE
                        ,Manifest.permission.CAMERA)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        /**
                         * aBoolean
                         * 返回 true 已授予权限
                         * 返回 false 未授予权限
                         */
                        Log.d("rcl", "==onNext: =="+aBoolean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
